package com.itheima.controller;

import com.itheima.health.resource.aliyunoss.AliyunUtils;
import com.itheima.health.resource.entity.QueryPageBean;
import com.itheima.health.resource.entity.Result;
import com.itheima.health.resource.pub.MessageConstant;
import com.itheima.health.resource.pub.RedisConstant;
import com.itheima.health.resource.redis.RedisConfig;
import com.itheima.health.resource.redis.RedisDemo;
import com.itheima.health.resource.upload.UploadUtils;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.SetmealService;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("setmeal")
public class SetmealController {


    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Reference
    private SetmealService setmealService;


    @RequestMapping("upload.do")
    public Result upload(@RequestParam("imgFile") MultipartFile file, HttpSession session){
        try {
            //  文件上oss上 并且uudi名称 回送给前端
            String originalFilename = file.getOriginalFilename();
            String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);
            //   文件上传到oss
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidfilename);
            //   文件存储redis   set 中
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,uuidfilename);//  文件添加到redis缓存中
            //  memeber(key)
            return  new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,uuidfilename);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

    }

    //新增页面数据和更新编辑数据
    @RequestMapping("add.do")
    public Result add(@RequestBody Setmeal setmeal,@RequestParam("checkgroupIds") Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
            return new Result(true, MessageConstant.UPDATE_SETEMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_SETEMEAL_FAIL);
        }
    }


    //  有条件和无条件查询 都是同一个方法
    @RequestMapping("findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map map = setmealService.findPageByContion(queryPageBean);
            return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }
    // //有条件和无条件都由该方法实现
    // //分页查询数据
    // @RequestMapping("findPage.do")
    // public Result findPage(@RequestBody QueryPageBean queryPageBean){
    //     try {
    //
    //         Map map = checkItemService.findPageByCondition(queryPageBean);
    //
    //
    //
    //         return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //         return new Result(true, MessageConstant.QUERY_CHECKITEM_FAIL);
    //     }
    // }
    //
    //
    // //删除数据
    // @RequestMapping("deleteCheckItemById.do")
    // public Result deleteCheckItemById(@RequestParam("id") Integer id){
    //     try {
    //
    //         //获取对象
    //         CheckItem checkItem = new CheckItem();
    //         //设置选中当行的id删除之后把is_delet的值改成1
    //         checkItem.setId(id);
    //         checkItem.setIs_delete(1);
    //
    //         checkItemService.updateById(checkItem);
    //         return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //         return new Result(true, MessageConstant.DELETE_CHECKITEM_FAIL);
    //     }
    // }
    //
    //
    // //  查询所有的检查项信息
    // @RequestMapping("findAll")
    // public Result findAll(){
    //     try {
    //         List<CheckItem> list =  checkItemService.findAll();
    //         return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //         return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    //     }
    //
    // }
    //
    //
    // //  根据检查组的id  查询对应检查项ids
    // @RequestMapping("findCheckItemIdsByGroupId.do")
    // public Result findCheckItemIdsByGroupId(@RequestParam("id") int id){
    //     try {
    //         List<Integer> list =  checkItemService.findCheckItemIdsByGroupId(id);
    //         return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //         return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    //     }
    //
    // }


}
